package pw.goyd.db;

import pw.goyd.game.constant.setting.ServerSetting;
import pw.goyd.game.server.Log;

import java.sql.*;

/**
 * 废弃了 用mybatis代替
 *
 * @author StanWind
 */
public class DBUtil implements ServerSetting {


    /**
     * 获取数据库链接
     *
     * @return Connection
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");    // Driver
            conn = DriverManager.getConnection("jdbc:mysql://" + M_ADDR + ":"
                    + M_PORT + "/" + M_DBNAME, M_ROOT, M_PWD);    // URL
        } catch (ClassNotFoundException e) {
            Log.getLogger().error(e.toString());
        } catch (SQLException e) {
            Log.getLogger().error(e.toString());
        }
        return conn;
    }

    /**
     * 关闭所有资源
     *
     * @param rs
     * @param stmt
     * @param conn
     */
    public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            Log.getLogger().error(e.toString());
        }
    }
}